/**
 * 条件：个人信息页面中
 * @returns
 */
$(function(){
	console.log("页面加载完成事件");
	$.ajax({
		url:"DataTable/findPersonalInformationDisplay",
		type:"post",
		dataType:"json",
		success:function(map){
			var bloodtypeList=map.bloodtypeList;
			var cultureList=map.cultureList;
			var faceList=map.faceList;
			var healthList=map.healthList;
			var religiousList=map.religiousList;
			var workingconditionsList=map.workingconditionsList;
			var maritalstatusList=map.maritalstatusList;
//			console.log(bloodtypeList);
			var bloodtype_select = document.getElementById("bloodtype");
			var culture_select = document.getElementById("culture");
			var face_select = document.getElementById("face");
			var health_select = document.getElementById("health");
			var religious_select = document.getElementById("religious");
			var workingconditions_select = document.getElementById("workingconditions");
			var maritalstatus_select = document.getElementById("maritalstatus");
			//消除下拉框
			bloodtype_select.innerHTML = "<option value='0'>选择</option>";
			culture_select.innerHTML = "<option value='0'>选择</option>";
			face_select.innerHTML = "<option value='0'>选择</option>"
			health_select.innerHTML = "<option value='0'>选择</option>";
			religious_select.innerHTML = "<option value='0'>选择</option>";
			workingconditions_select.innerHTML = "<option value='0'>选择</option>"
			maritalstatus_select.innerHTML = "<option value='0'>选择</option>"
			//加载婚姻状况
			for(var i=0;i<maritalstatusList.length;i++){
				var opt=document.createElement("option");
				opt.innerText=maritalstatusList[i].name;
				opt.setAttribute("value",maritalstatusList[i].id);
				maritalstatus_select.appendChild(opt);
			}
			//加载就业状况
			for(var i=0;i<workingconditionsList.length;i++){
				var opt=document.createElement("option");
				opt.innerText=workingconditionsList[i].name;
				opt.setAttribute("value",workingconditionsList[i].id);
				workingconditions_select.appendChild(opt);
			}
			//加载宗教信仰
			for(var i=0;i<religiousList.length;i++){
				var opt=document.createElement("option");
				opt.innerText=religiousList[i].name;
				opt.setAttribute("value",religiousList[i].id);
				religious_select.appendChild(opt);
			}
			//加载健康状况
			for(var i=0;i<healthList.length;i++){
				var opt=document.createElement("option");
				opt.innerText=healthList[i].name;
				opt.setAttribute("value",healthList[i].id);
				health_select.appendChild(opt);
			}
			//加载血型
			for(var i=0;i<bloodtypeList.length;i++){
				var opt=document.createElement("option");
				opt.innerText=bloodtypeList[i].name;
				opt.setAttribute("value",bloodtypeList[i].id);
				bloodtype_select.appendChild(opt);
			}
			//加载文化程度
			for(var i=0;i<cultureList.length;i++){
				var opt=document.createElement("option");
				opt.innerText=cultureList[i].name;
				opt.setAttribute("value",cultureList[i].id);
				culture_select.appendChild(opt);
			}
			//加载政治面貌
			for(var i=0;i<faceList.length;i++){
				var opt=document.createElement("option");
				opt.innerText=faceList[i].name;
				opt.setAttribute("value",faceList[i].id);
				face_select.appendChild(opt);
			}
		},
		error:function(){
			console.log("数据返回失败");
		}		
	});

	var index1 = false;
	var index2 = false;
	var index3 = false;
	var index4 = false;
	var index5 = false;
	var index6 = false;
	var index7 = false;
	
	
	/**
	 * 体重判断
	 */
	$("#weights").blur(function(){
		var tele= $("#weights").val();
		var patrn= /^(0(\.\d{1}){0,1}|[1-8]\d{1,3}(\.\d{1}){0,1}|9\d{1,2}(\.\d{1}){0,1}|999(\.0){0,1}|.{0})$/;
		if(patrn.test(tele)==false){
			$("#weighte").html("请填写以公斤为单位的数字").css('color','red');
			index6 = false;
		}else if(tele == ""){
			$("#weighte").html("请填写以公斤为单位的数字").css('color','red');
			index6 = false;
		}else{
			$("#weighte").html("输入正确").css('color','green');
			index6 = true;
		}
	})
	
	/**
	 * 身高判断
	 */
	$("#height").blur(function(){
		var tele= $("#height").val();
		var patrn=/^(0{1}|[1-9]\d{0,3})$/;
		if(patrn.test(tele)==false){
			$("#heights").html("请填写以厘米(cm)为单位的数字").css('color','red');
			index7 = false;
		}else{
			$("#heights").html("输入正确").css('color','green');
			index7 = true;
		}
	})
	
	/**
	 * 验证姓名是否为空
	 */
	$("#userName").blur(function(){
		var tele= $("#userName").val();
		var patrn="";
		if(tele ==""){
			$("#userNames").html("请填写姓名").css('color','red');
			index4 = false;
		}else{
			$("#userNames").html("输入正确").css('color','green');
			index4 = true;
		}
	})

	/**
	 * 验证紧急联系人姓名是否为空
	 */
	$("#emergencycontact").blur(function(){
		var tele= $("#emergencycontact").val();
		var patrn="";
		if(tele ==""){
			$("#emergencycontacts").html("请填写姓名").css('color','red');
			index5 = false;
		}else{
			$("#emergencycontacts").html("输入正确").css('color','green');
			index5 = true;
		}
	})

	/**
	 * 验证身份证号是否正确
	 */
	$("#identitynumber").blur(function(){
		var tele= $("#identitynumber").val();
		var patrn=/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
		if(patrn.test(tele)==false){
			$("#identitynumbers").html("请填写正确的身份证号").css('color','red');
			index2 = false;
		}else{
			$.ajax({
				url:"FloatingPopulation/selectCountCitizenshipNumber",
				type:"post",
				dataType:"json",
				data:{
					citizenshipNumber:tele
				},
				success:function(json){ 
					if( json.count != 0 ){
						$("#identitynumbers").html("输入正确").css('color','green');
						index2 = true;
					}else{
						$("#identitynumbers").html("身份证重复").css('color','red');
					}
				},
				error:function(){
					window.location.href="404.html";
				}
			});
		}
	})

	/**
	 * 验证手机号是否正确1
	 */
	$("#phone").blur(function(){
		var tele= $("#phone").val();
		var patrn=/^(13[0-9]|15[0-9]|18[0-9])\d{8}$/;
		if(patrn.test(tele)==false){
			$("#spPhone").html("请填写正确的手机号").css('color','red');
			index1 = false;
		}else{
			$.ajax({
				url:"FloatingPopulation/selectCountMobilePhone",
				type:"post",
				dataType:"json",
				data:{
					citizenshipNumber:tele
				},
				success:function(json){ 
					if( json.count != 0 ){
						$("#spPhone").html("输入正确").css('color','green');
						index1 = true;
					}else{
						$("#spPhone").html("手机号重复").css('color','red');
					}
				},
				error:function(){
					window.location.href="404.html";
				}
			});
		}
	})

	/**
	 * 验证手机号是否正确3
	 */
	$("#phones").blur(function(){
		var tele= $("#phones").val();
		var patrn=/^(13[0-9]|15[0-9]|18[0-9])\d{8}$/;
		if(patrn.test(tele)==false){
			$("#spPhones").html("请填写正确的手机号").css('color','red');
			index3 = false;
		}else{
			$("#spPhones").html("输入正确").css('color','green');
			index3 = true;
		}
	})

	/**
	 * 个人信息中点击保存的添加方法
	 */
	$("#btn_personal").click(function(){
		if(index4 == true && index5 == true && index2 == true && index1 == true && index3 == true && index6 == true && index7 == true){
			$.ajax({
				url:"personalInformation/addPersonal",
				type:"post",
				dataType:"json",
				data:$('#form_personal').serialize(),
				success:function(json){ 
					console.log("添加成功！！");
					//存放cookie-存放到cookie
	            	$.cookie("province",json.liveUser.province, { expire:1  } );
	            	$.cookie("city",json.liveUser.city, { expire:1  } );
	            	$.cookie("county",json.liveUser.county, { expire:1  } );
	            	$.cookie("township",json.liveUser.township, { expire:1  } );
	            	$.cookie("village",json.liveUser.village, { expire:1  } );
	            	$.cookie("gates",json.liveUser.gates, { expire:1  } );
					//跳转到配偶信息
					window.location.href="spouseAdd.html";
				},
				error:function(){
					window.location.href="404.html";
				}
			});
		}
	})
	
	
})